﻿using System;
using System.Windows;
using MediaBrowserSL.RIA.ViewModels;
using MediaBrowserSL.RIA.Views;

namespace MediaBrowserSL.RIA.MvvmUtils.UI
{
    public class WindowZoomPictureVisualizer : IWindowZoomPictureVisualizer
    {
        private ChildWindowZoomPicture dlg;
        private bool isShow;
        private Action<object> closeCallback;

        public void Show(Action<object> closeCallback)
        {
            this.closeCallback = closeCallback;
            if (!isShow)
            {
                dlg = new ChildWindowZoomPicture();
                dlg.LayoutUpdated += this.GoToInitialState;
                dlg.Closed += new EventHandler(dlg_Closed);
                EventHandler handler = null;
                handler = delegate
                {
                    ((ViewModelBase)dlg.DataContext).RequestClose -= handler;
                    dlg.Close();
                };
                ((ViewModelBase)dlg.DataContext).RequestClose += handler;
                isShow = true;
                dlg.Show();
            }
        }

        void dlg_Closed(object sender, EventArgs e)
        {
            isShow = false;
            closeCallback(dlg);
        }

        private void GoToInitialState(object sender, EventArgs eventArgs)
        {
            VisualStateManager.GoToState(dlg, "AtFront", false);
            dlg.LayoutUpdated -= this.GoToInitialState;
        }

        public void GoToBack()
        {
            VisualStateManager.GoToState(dlg, "AtBack", true);
        }

        public void GoToFront()
        {
            VisualStateManager.GoToState(dlg, "AtFront", true);
        }
    }
}
